import React from 'react';
import action from '../../../store/action';
import {GoPagePush} from '../../../assets/js/utils';
import {connect} from 'react-redux';
import request from "../../../assets/js/utils/request";
import HeadNavComponent from "../../../components/headNav";
import {Modal} from 'antd-mobile';
import Css from '../../../assets/css/user/ucenter/index.css';
class user extends React.Component{
    constructor(props){
        super(props);
        this.state={
            aName:'',
            aImg:require('../../../assets/images/commen/head.png'),
            aPoints:0,
            aUserBtn:'登录/注册'
        }
    }
    componentDidMount(){
        this.getUserData();
    }
    //获取会员信息
    getUserData(){
        let isLogin=this.props.state.redLogin.isLogin;
        if(isLogin){
            let sUrl=window.base.config.baseUrl+'/user/myinfo/userinfo/uid/'+this.props.state.redLogin.uid+'?token='+window.base.config.token;
            request(sUrl,'get').then((res)=>{
                if (res.code===200){
                    this.setState({
                        aName:res.data.nickname,
                        aImg:res.data.head!==''?res.data.head:this.state.aImg,
                        aPoints:res.data.points,
                        aUserBtn:'安全退出'
                    })
                }
            })
        }
    }
    //退出登录
    outLogin(){
        let isLogin=this.props.state.redLogin.isLogin;
        if(isLogin){
            Modal.alert('确定要删除吗？', '', [
                { text: '取消', onPress: () => console.log('cancel') },
                { text: '确定', onPress: () =>{
                        let sUrl=window.base.config.baseUrl+'/home/user/safeout?token='+window.base.config.token;
                        request(sUrl,'post',{uid:this.props.state.redLogin.uid}).then(res=>{
                            if (res.code===200){
                                this.props.dispatch(action.actLogin.actOutLogin({
                                    uid:'',
                                    username:'',
                                    authtoken:'',
                                    isLogin:false,
                                    success:()=>{
                                        GoPagePush(this.props,'login/index')
                                    }
                                }));
                                this.props.dispatch(action.actCart.clearCart())

                            }
                        })
                    }
                },

            ]);
        }else {
            GoPagePush(this.props,'login/index')
        }

    }
    goPages(url){
        GoPagePush(this.props,url)
    }
    //解决内存泄漏
    componentWillUnmount(){
        this.setState=(state,callback)=>{
            return ;
        }
    }
    render(){

        return(
            <div className={Css['main']}>
                <HeadNavComponent title={'会员中心'} ></HeadNavComponent>
                <div className={Css['user_info']}>
                    <div className={Css['user_info_img']} >
                        <img src={this.state.aImg} alt={this.state.aName}/>
                    </div>
                    <div className={Css['user_info_font']}>
                        <div className={Css['user_info_font_name']}>昵称:{this.state.aName}</div>
                        <div className={Css['user_info_font_Integral']}>我的积分：{this.state.aPoints}</div>
                    </div>
                </div>
                <div className={Css['user_oder']}>
                    <div className={Css['user_oder_more']}>
                        <p>全部订单</p>
                        <p onClick={this.goPages.bind(this,'order/order?status=all')}>查看全部订单 <i className={'goBackIcn'}></i></p>
                    </div>
                    <div className={Css['user_wait']}>
                        <div className={Css[['uesr_wait_wrap']]} onClick={this.goPages.bind(this,'order/order?status=0')}>
                            <i className={'paidIcn'}></i>
                            <p>待支付</p>
                        </div>
                        <div className={Css[['uesr_wait_wrap']]} onClick={this.goPages.bind(this,'order/order?status=1')}>
                            <i className={'receivingIcn'}></i>
                            <p>待收货</p>
                        </div>
                        <div className={Css[['uesr_wait_wrap']]} onClick={this.goPages.bind(this,'order/review?status=2')}>
                            <i className={'appraisalsIcn'}></i>
                            <p>待评价</p>
                        </div>
                    </div>
                </div>
                <div className={Css['user_list']}>
                    <div className={Css['user_list_grop']} onClick={this.goPages.bind(this,'profile/index')}>
                        <p>个人资料</p>
                        <i className={'goBackIcn'}></i>
                    </div>
                    <div className={Css['user_list_grop']} onClick={this.goPages.bind(this,'user/address/index')}>
                        <p>收货地址</p>
                        <i className={'goBackIcn'}></i>
                    </div>
                    <div className={Css['user_list_grop']} onClick={this.goPages.bind(this,'user/mobile/index')}>
                        <p>绑定手机</p>
                        <i className={'goBackIcn'}></i>
                    </div>
                    <div className={Css['user_list_grop']} onClick={this.goPages.bind(this,'user/mobpwd/index')}>
                        <p>修改密码</p>
                        <i className={'goBackIcn'}></i>
                    </div>
                    <div className={Css['user_list_grop']} onClick={this.goPages.bind(this,'user/myfav/index')}>
                        <p>我的收藏</p>
                        <i className={'goBackIcn'}></i>
                    </div>
                </div>
                <div className={Css['user_btn']} onClick={this.outLogin.bind(this)}>{this.state.aUserBtn}</div>
            </div>
        )
    }
}
export default connect((state)=>{
    return{
        state:state
    }
})(user);